import 'package:flutter/material.dart';
import 'package:duo_zui/widget/search_text_field_widget.dart';
import 'package:duo_zui/util/screen_utils.dart';
import 'package:duo_zui/router.dart';

const double _kTabHeight = 46.0;
const double _kTextAndIconTabHeight = 42.0;

class HomeTabBar extends StatefulWidget implements PreferredSizeWidget {
  final TabBar tabBar;
  final double translate;

  HomeTabBar({Key key, this.tabBar, this.translate}) : super(key: key);

  @override
  _HomeTabBarState createState() => _HomeTabBarState();

  @override
  Size get preferredSize {
    print('preferredSize');
    for (Widget item in tabBar.tabs) {
      if (item is Tab) {
        final Tab tab = item;
        if (tab.text != null && tab.icon != null)
          return Size.fromHeight(
              _kTextAndIconTabHeight + tabBar.indicatorWeight);
      }
    }
    return Size.fromHeight(_kTabHeight + tabBar.indicatorWeight);
  }
}

class _HomeTabBarState extends State<HomeTabBar> {
  double get allHeight => widget.preferredSize.height;

  @override
  Widget build(BuildContext context) {
    return Stack(
          children: <Widget>[
            Positioned(
              ///搜索框
              left: 15.0,
              right: ScreenUtils.screenW() * 0.75 - 3.0,
              top: getTop(widget.translate) + 3,
              child: getOpacityWidget(Container(
                padding: const EdgeInsets.only(
                    top: 3.0, bottom: 3.0, right: 10.0, left: 5.0),
                decoration: BoxDecoration(
                    color: const Color.fromARGB(245, 236, 236, 236),
                    borderRadius: BorderRadius.all(Radius.circular(17.0))),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.search,
                      color: const Color.fromARGB(255, 128, 128, 129),
                    ),
                    Expanded(
                      child: GestureDetector(
                        child: Align(
                          alignment: Alignment(0.0, 0.0),
                          child: Text(
                            '搜索',
                            style: TextStyle(
                                fontSize: 16.0,
                                color: const Color.fromARGB(255, 192, 192, 192)),
                          ),
                        ),
                        onTap: () {
                          MyRouter.push(context, MyRouter.searchPage, '搜索嘟嘴APP试一试');
                        },
                      ),
                    )
                  ],
                ),
              )),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 5.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 2,
                    child: Container(),
                  ),
                  Expanded(
                    flex: 5,
                    child: widget.tabBar,
                  ),
                  Expanded(
                    flex: 2,
                    child: Container(),
                  ),
                ],
              ),
            )
          ],
    );
  }

  double getTop(double translate) {
    return Tween<double>(begin: allHeight, end: 0.0)
        .transform(widget.translate);
  }

  Widget getOpacityWidget(Widget child) {
    if (widget.translate == 1) {
      return child;
    }
    return Opacity(
      opacity: const Interval(0.0, 1.0, curve: Curves.fastOutSlowIn)
          .transform(widget.translate),
      child: child,
    );
  }
}
